import javax.sound.midi.Soundbank;

class ListNode{
    public int val;
    public ListNode prev;
    public ListNode next;
    public ListNode(int data){
        this.val=data;
    }
}
public class MyLinKedList {
    ListNode head=null;
    ListNode last=null;
    //傀儡节点
    public MyLinKedList(){
        this.head=new ListNode(-1);
    }
    //得到链表长度
    public int size(){
        int count=0;
        ListNode cur=this.head.next;
        while(cur!=null ){
            cur=cur.next;
            count++;
        }
        return count;
    }
    //打印链表
    public void display(){
        if(this.head.next==null){
            System.out.println("链表为空无法打印");
            return ;
        }
        ListNode cur=this.head.next;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }
    //查找链表中是否包含key节点
    public boolean contains(int key) {
        if (this.head.next == null) {
            return false;
        }
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //头插
    public void addFirst(int data){
        ListNode Data=new ListNode(data);
        if(this.head.next==null){
            this.head.next=Data;
            Data.prev=this.head;
            this.last=Data;
            return ;
        }
        this.head.next.prev=Data;
        Data.next=this.head.next;
        this.head.next=Data;
        Data.prev=this.head;
    }
    //尾插
    public void addLast(int data){
        ListNode Data=new ListNode(data);
        if(this.last==null){
            this.last=Data;
            this.last.prev=this.head;
            this.head.next=this.last;
            return ;
        }
        Data.prev=this.last;
        this.last.next=Data;
        this.last=Data;
    }
    //在链表的任意位置插入一个节点
    public void addIndex(int index,int data){
        ListNode Data=new ListNode(data);
        if(index<0||index>this.size()){
            System.out.println("下标违法");
            return ;
        }
        if(index==0){
            this.addFirst(data);
            return ;
        }
        if(index==this.size()){
            this.addLast(data);
            return ;
        }
        ListNode cur=this.head.next;
        while(index>0){
            index--;
            cur=cur.next;
        }
        cur.next.prev=Data;
        Data.next=cur.next;
        Data.prev=cur;
        cur.next=Data;
    }
    //删除链表中第一个出现的key节点
    public void remove(int key){
        if(this.head.next==null){
            System.out.println("链表为空无法删除");
            return;
        }
        ListNode cur=this.head.next;
        while(cur!=null){
            if(cur.val==key){
                if(cur==this.last){
                    this.last.prev.next=null;
                    this.last=this.last.prev;
                    cur.prev=null;
                    cur=null;
                }else{
                    cur.next.prev=cur.prev;
                    cur.prev.next=cur.next;
                    cur.next=null;
                    cur.prev=null;
                    cur=null;
                }
                break;
            }else{
                cur=cur.next;
            }
        }
    }
    ///删除链表中的key节点
    public void removeAllKey(int key){
        ListNode cur=this.head.next;
        while(cur!=null){
            if(cur.val==key){
                if(cur==this.last){
                    this.last.prev.next=null;
                    this.last=this.last.prev;
                }else{
                    cur.next.prev=cur.prev;
                    cur.prev.next=cur.next;
                }
            }
            cur=cur.next;
        }
    }
    //链表销毁
    public void clear(){
        ListNode cur=this.head.next;
        while(cur!=null){
            ListNode curnext=cur.next;
            cur.prev=null;
            cur.next=null;
            cur=cur.next;
        }
        this.head.next=null;
        this.last=null;
    }
}
